#!/usr/bin/env perl
use warnings;
use strict;

# Given a file like probe_answers.txt (in netflix/data) on standard in and a
# file like big.movies.ids (see the Makefile) as the only argument, produce
# stats on how many movies in the probe set we matched and how many ratings
# that corresponds to.

scalar(@ARGV) == 1 or die "need list of matched movie ids as argument";
my $matched_id_file = $ARGV[0];
-e$matched_id_file  or die "input id file $matched_id_file not found";

open(my $fh, "<$matched_id_file");
my @matched_ids = <$fh>;
close($fh);

# Use a set of integers for reasonable efficiency.
@matched_ids = map { int($_) } @matched_ids;
my %matched_ids = map { $_ => 1 } @matched_ids;

# Read probe input and write output as we go.
my $movies = 0;
my $ratings = 0;
my $probe_movies = 0;
my $probe_ratings = 0;
my $probe_ratings_total = 0;
my $curr_movie_matched;
while (<STDIN>) {
  chomp;
  if (/(\d+):/) {
    # This is a movie id.
    $movies += 1;
    if (defined($matched_ids{int($1)})) {
      $probe_movies += 1;
      $curr_movie_matched = 1;
    } else {
      $curr_movie_matched = 0;
    }
  } else {
    defined($curr_movie_matched) or die "expected probe answers to start with movie id";
    /\d+,(\d+),.*/ or die "unexpected rating line format ($_)";
    $ratings += 1;
    if ($curr_movie_matched) {
      $probe_ratings_total += $1;
      $probe_ratings += 1;
    }
  }
}

print "movies: $movies\n";
print "ratings: $ratings\n";
print "matched movies: $probe_movies (".($probe_movies/$movies*100)."%)\n";
print "matched ratings: $probe_ratings (".($probe_ratings/$ratings*100)."%)\n";
print "mean ratings per matched movie: "
  .($probe_ratings / $probe_movies)."\n";
print "mean ratings per unmatched movie: "
  .(($ratings - $probe_ratings) / ($movies - $probe_movies))."\n";
print "probe ratings mean: ".($probe_ratings_total / $probe_ratings)."\n";

# Copyright (c) 2009 John Lees-Miller
# 
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

